package org.inspursc.s0517.health.evaluation.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import io.swagger.annotations.ApiParam;
import org.inspursc.s0517.health.common.entity.SysUser;
import org.inspursc.s0517.health.common.enums.UserTypeEnum;
import org.inspursc.s0517.health.common.util.ResponseUtil;
import org.inspursc.s0517.health.common.util.SpringSecurityUtils;
import org.inspursc.s0517.health.common.vo.ResponseVO;
import org.inspursc.s0517.health.evaluation.dto.*;
import org.inspursc.s0517.health.evaluation.service.InsSurveyAnswerService;
import org.inspursc.s0517.health.evaluation.utils.ReadTextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * institution assessment controller
 */
@RestController
@Api(description = "institution assessment")
@RequestMapping(value = "/insAssessment")
public class InsAssessmentController {

    @Autowired
    InsSurveyAnswerService insSurveyAnswerService;

    @ApiOperation("save institution assessment api")
    @RequestMapping(value = "/saveInsAssessment", method = RequestMethod.POST)
    public ResponseEntity<ResponseVO> saveInsAssessment(@RequestBody InsSurveyAnswerDTO insSurveyAnswerDTO, HttpServletRequest request) {
        try {
            InsSurveyAnswerGradeDTO insSurveyAnswerGradeDTO = insSurveyAnswerService.saveAnswer(insSurveyAnswerDTO, request);
            return ResponseUtil.success("successeed!!!!!!", insSurveyAnswerGradeDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error(e.getMessage(),null);
        }
    }

    @ApiOperation("提交离线评估")
    @RequestMapping(value = "/uploadOffLineSurvey", method = RequestMethod.POST)
    public ResponseEntity<ResponseVO> uploadOffLineSurvey(HttpServletRequest request) {
        String txt = ReadTextUtil.readTextToStr(request);
        InsSurveyAnswerDTO answerDTO = JSON.parseObject(txt, InsSurveyAnswerDTO.class);
        try {
            InsSurveyAnswerGradeDTO dto = insSurveyAnswerService.saveAnswer(answerDTO, request);
            return ResponseUtil.success("successeed!!!!!!", dto);
        } catch (Exception e) {
            return ResponseUtil.error(e.getMessage(), null);
        }
    }

    @ApiOperation("根据ID删除机构评估报告")
    @RequestMapping(value = "/deleteAnswerById", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> deleAssessmentById(@ApiParam("调查报告Id") @RequestParam(value = "id") Integer id, HttpServletRequest request) {
        try {
            SysUser user = SpringSecurityUtils.getCurrentUser(request);
            // 如果不是系统管理员
            if ( !UserTypeEnum.ADMIN.getCode().equals(user.getUserType())) {
                return ResponseUtil.error("只允许系统管理员删除评估信息！");
            }

            if(insSurveyAnswerService.deleteAnswerById(id)){
                return ResponseUtil.success("删除成功",true);
            } else {
                return ResponseUtil.error("删除失败！");
            }
        } catch (Exception e) {
            return ResponseUtil.error("删除失败！" + e.getMessage());
        }
    }

    @ApiOperation("根据Id查询调查结果")
    @RequestMapping(value = "/findOneById", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> getInsAssessResultById(@ApiParam("调查结果Id") @RequestParam(value = "id") Integer id) {
        try {
            InsSurveyAnswerGradeDTO dto = insSurveyAnswerService.findOneById(id);
            return ResponseUtil.success(dto);
        } catch (Exception e) {
            return ResponseUtil.error(e.getMessage());
        }
    }

    @ApiOperation("根据调查结果Id 获取调查详情")
    @RequestMapping(value = "/getAnswerDetailById", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> getAnswerDetailById(@ApiParam("回答问卷Id") @RequestParam(value = "id") Integer id, HttpServletRequest request) {
        try {
            AssessmentAnswerDateilsDTO dto = insSurveyAnswerService.getAnswerDetailById(id, request);
            return ResponseUtil.success(dto);
        } catch (Exception e) {
            return ResponseUtil.error(e.getMessage());
        }
    }
}
